// src/router/index.js
import { createRouter, createWebHistory } from 'vue-router';
import { ElMessage } from 'element-plus';

const router =createRouter({
  history:createWebHistory(),
  routes:[
      {
        path: '/home',
        name: 'Home',
        component:()=>import('../views/HomeView.vue'),
        meta:{
          isAuth:true
        }
      },
      {
          path: '/login',
          name: 'login',
          component:()=>import('../views/LoginView.vue')
      },
      {
          path: '/register',
          name: 'register',
          component:()=>import('../views/RegisterView.vue')
      },
  ],
});

// 路由拦截
router.beforeEach((to, from, next) => {
  if(to.meta.isAuth){
    if(localStorage.getItem('token')){
      next();
    }else {
      ElMessage.error('请先登录');
      router.push('/login');
    }
  }else {
    next();
  }
});

export default router;
